﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Data.OleDb;

namespace MyBusiness
{
    public class WebInfoOperation
    {
        Database dbGloab = BasicOperate.CreateDataBase();

        /// <summary>
        /// 获取通用信息
        /// </summary>
        /// <param name="strKeyId"></param>
        /// <returns></returns>
        public DataTable GetPubInfo(string strKeyId)
        {
            string strSql = string.Format(@"select * from st_other where id='{0}'", strKeyId);
            return dbGloab.ExecuteDataSet(CommandType.Text, strSql).Tables[0];
        }

        /// <summary>
        /// 获取json数据
        /// </summary>
        /// <param name="strKeyId"></param>
        /// <returns></returns>
        public string GetPubDetail(string strKeyId)
        {
            string strSql = string.Format(@"select * from st_other where id='{0}'", strKeyId);
            DataTable dtSource = dbGloab.ExecuteDataSet(CommandType.Text, strSql).Tables[0];

            string strReturn = string.Empty;
            if (dtSource.Rows.Count > 0)
            {
                DataToJson _djson = new DataToJson();
                strReturn = _djson.BindJson(dtSource);
            }
            return strReturn;
        }

        /// <summary>
        /// 根据类型获取所有信息
        /// </summary>
        /// <param name="strType"></param>
        /// <returns></returns>
        public DataTable GetPubInfoByType(string strTable,string strWhere,string strOrderBy)
        {
            string strSql = string.Format(@"select t.* from {0} t where 1=1",strTable);

            if (!string.IsNullOrEmpty(strWhere))
            {
                strSql+= string.Format(" and {0}", strWhere);
            }
            if (!string.IsNullOrEmpty(strWhere))
            {
                strSql += strOrderBy; ;
            }
            return dbGloab.ExecuteDataSet(CommandType.Text, strSql).Tables[0];
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="strId"></param>
        public void DeleteArticle(string strId)
        {
            string strSql = string.Format(@"delete from st_other where id='{0}'", strId);
            dbGloab.ExecuteNonQuery(CommandType.Text, strSql);
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="strTitle"></param>
        /// <param name="strID"></param>
        /// <param name="strContent"></param>
        public void UpdatePubInfo(string strTitle,string strID,string strContent)
        {
            string strSql = string.Format(@"update st_other set CONTENT=@conParam,title='{0}' where id='{1}'", strTitle, strID);

            DbCommand dcTemp = dbGloab.GetSqlStringCommand(strSql);
            dbGloab.AddInParameter(dcTemp, "@conParam", DbType.String, strContent);

            dbGloab.ExecuteNonQuery(dcTemp);
        }
        
        /// <summary>
        /// 插入数据
        /// </summary>
        /// <param name="strTitle"></param>
        /// <param name="strID"></param>
        /// <param name="strType"></param>
        /// <param name="strContent"></param>
        public void InsertPubInfo(string strTitle, string strID,string strType, string strContent)
        {
            string strSql = string.Format(@"insert into st_other(TITLE,CONTENT,TYPE,id,FBSJ)  VALUES ('{0}',@conParam,'{1}','{2}',Date())", strTitle,strType, strID);

            //OleDbParameter opTemp = new OleDbParameter("@conParam", OleDbType.LongVarChar);
            //opTemp.Value = strContent;
            //dcTemp.Parameters.Add(opTemp);

            DbCommand dcTemp = dbGloab.GetSqlStringCommand(strSql);
            dbGloab.AddInParameter(dcTemp, "@conParam", DbType.String, strContent);
            dbGloab.ExecuteNonQuery(dcTemp);
        }
    }
}
